﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security;

using MP.Core.Authentication;
using MP.Core.Config;
using MP.LayerHelper.MP_OBS.BLL;

public partial class Member_Control_AddRole : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.ROLE);
        if (!IsPostBack)
        {
            LoadData();
            addrole.Visible = IsAction(EnumActions.ADD);
        }
    }

    void LoadData()
    {
        DataTable tbl = MPRoleProvider.CreateInstant().GetAll();
        tbl.DefaultView.Sort = "RoleName";
        repRoles.DataSource = tbl.DefaultView;
        repRoles.DataBind();
    }

    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Roles.CreateRole(txtRoleName.Text.Trim());
            Response.Redirect(Request.RawUrl);
        }
    }

    protected void validRole_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
        if (Roles.RoleExists(txtRoleName.Text.Trim()))
        {
            args.IsValid = false;
            validRole.ErrorMessage = "Trùng tên quyền";
        }
    }

    protected void repRoles_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string role = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "enable":
                MPRoleProvider.CreateInstant().UpdateEnable(role);
                Response.Redirect(Request.RawUrl);
                break;
            case "del":
                string[] s = role.Split('|');
                Guid roleId = new Guid(s[0]);
                if (MbRolesManager.CreateInstant().CheckDeleteRole(roleId))
                {
                    Roles.DeleteRole(s[1]);
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    validRole.IsValid = false;
                    validRole.ErrorMessage = "Quyền đang được sữ dụng, không thể xóa";
                }
                break;
        }
        
    }

    public string viewImgEnable(bool IsEnable)
    {
        string img = "";

        if (IsEnable)
            img = "accept";
        else
            img = "block";

        return img;
    }
}
